package com.softstao.guoyu.ui.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.softstao.guoyu.R;
import com.softstao.guoyu.model.shop.CartGoods;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewBaseAdapter;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewHolder;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.NumberView;

import java.util.List;

/**
 * Created by vaikus on 2017/7/22.
 */

public class CartAdapter extends RecycleViewBaseAdapter<CartGoods> {
    private CartListener cartListener;
    public CartAdapter(List<CartGoods> datas) {
        super(datas, R.layout.layout_cart_list_item);
    }

    @Override
    public void convert(RecycleViewHolder holder, CartGoods cartGoods) {
        RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        holder.itemView.setLayoutParams(layoutParams);
        if (cartGoods.getImg()!=null && !cartGoods.getImg().equals("")){
            Glide.with(mContext).load(cartGoods.getImg()).into((ImageView) holder.getView(R.id.goods_im));
        }else {
            ((ImageView) holder.getView(R.id.goods_im)).setImageResource(R.mipmap.default_im);
        }
        holder.setText(R.id.name,cartGoods.getName())
                .setText(R.id.price,mContext.getResources().getString(R.string.rmb) + LZUtils.priceFormat(cartGoods.getPrice()))
                .setText(R.id.total_price,mContext.getResources().getString(R.string.rmb) + LZUtils.priceFormat(cartGoods.getSubtotal()));
        if (((CheckBox)holder.getView(R.id.checkbox)).isChecked()==true) {
            ((CheckBox) holder.getView(R.id.checkbox)).setChecked(false);
        }
        ((CheckBox)holder.getView(R.id.checkbox)).setOnCheckedChangeListener((buttonView, isChecked) -> {
            if(cartListener!=null)
                cartListener.getChoiceData(holder.getAdapterPosition(),isChecked);
        });
        NumberView numberView = holder.getView(R.id.goods_num);
        numberView.setNumber(cartGoods.getNum(),0);
        numberView.setListener(new NumberView.OnNumberButtonClickListener() {
            @Override
            public void onDecreaseButtonClick(View view, int number, int preNumber) {
                if (cartListener!=null){
                    cartListener.subtractPrice(holder.getAdapterPosition(),((CheckBox)holder.getView(R.id.checkbox)).isChecked(),preNumber,number);
                }
            }

            @Override
            public void onIncreaseButtonClick(View view, int number) {
                if (cartListener != null){
                    cartListener.addPrice(holder.getAdapterPosition(),((CheckBox)holder.getView(R.id.checkbox)).isChecked());
                }
                numberView.setNumber(number,cartGoods.getBoxNum());
            }
        });
        holder.getView(R.id.del).setOnClickListener(v -> {
            if (cartListener != null){
                cartListener.del(holder.getAdapterPosition());
            }
        });
    }

    public interface CartListener{
        void getChoiceData(int position, boolean isChoice);
        void subtractPrice(int position, boolean isChecked,int preNumber, int number);
        void addPrice(int position,boolean isChecked);
        void del(int position);
    }

    public CartListener getCartListener() {
        return cartListener;
    }

    public void setCartListener(CartListener cartListener) {
        this.cartListener = cartListener;
    }
}
